<template>
  <el-card style="margin-bottom:20px;">
    <div slot="header" class="clearfix">
      <span>我的账号</span>
    </div>

    <div class="user-profile">
      <div class="box-center">
        <pan-thumb :image="myAvatar" :height="'100px'" :width="'100px'" :hoverable="false">
          <div> (˵ ͡° ͜ʖ ͡°˵)</div>
        </pan-thumb>
      </div>
      <div class="box-center">
        <div class="user-name text-center">{{ user.name }}</div>
        <div class="user-role text-center text-muted">{{ mappedRole }}</div>
      </div>
    </div>
    <div class="user-bio">
      <div class="user-skills user-bio-section">
        <div class="user-bio-section-header"><svg-icon icon-class="skill" /><span>统计</span></div>
        <div class="user-bio-section-body">
          <div class="progress-item">
            <span>产品个数</span>
            <el-progress :percentage="((userInfo.products / userInfo.max_products) * 100).toFixed(1)" />
            <div class="progress-number">{{ userInfo.products }} / {{ userInfo.max_products }}</div>
          </div>
          <div class="progress-item">
            <span>密钥个数</span>
            <el-progress :percentage="((userInfo.license_keys / userInfo.max_license_keys) * 100).toFixed(1)" />
            <div class="progress-number">{{ userInfo.license_keys }} / {{ userInfo.max_license_keys }}</div>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import PanThumb from '@/components/PanThumb'
import { getInfo } from "@/api/getinfo"; // 引入 API 方法

export default {
  components: { PanThumb },
  data() {
    return {
      myAvatar: '/frlIf.png',
      userInfo: {
        name: '',
        products: 0,
        license_keys: 0,
        license_keys_activated: 0,
        max_license_keys: 0,
        max_products: 0
      },
      loading: false
    }
  },
  mounted() {
    this.fetchInfo();
  },
  methods: {
    fetchInfo() {
      this.loading = true;
      getInfo()
        .then((response) => {
          if (response.success) {
            this.userInfo = {
              name: response.data.name,
              products: response.data.products,
              license_keys: response.data.license_keys,
              license_keys_activated: response.data.license_keys_activated,
              max_license_keys: response.data.max_license_keys,
              max_products: response.data.max_products
            };
          } else {
            this.$message.error("获取信息失败");
          }
        })
        .catch((error) => {
          console.error("获取信息失败:", error);
          this.$message.error("获取信息失败，请稍后重试");
        })
        .finally(() => {
          this.loading = false;
        });
    }
  },
  computed: {
    mappedRole() {
      const roleMap = {
        Developer: "开发者",
        Admin: "超级管理员"
      };
      return roleMap[this.user.role] || this.user.role;
    }
  },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          avatar: '',
          role: ''
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }

  .progress-number {
    text-align: center;
    font-size: 14px;
    color: #606266;
    margin-top: 10px;
  }
}
</style>
